package com.itheima.web.controller.factoryProduct;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.Factory;
import com.itheima.domain.cargo.FactoryExample;
import com.itheima.domain.cargo.FactoryProduct;
import com.itheima.domain.cargo.FactoryProductExample;
import com.itheima.service.cargo.FactoryProductService;
import com.itheima.service.cargo.FactoryService;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 */
@Controller
@RequestMapping("/cargo/factoryProduct")
public class FactoryProductController extends BaseController {

    @Reference
    private FactoryProductService factoryProductService;

    @Reference
    private FactoryService factoryService;


    //分页查询
    @RequestMapping(value = "/list", name = "公司页面分页展示")
    public String list(@RequestParam(name = "page", defaultValue = "1") int pageNum,
                       @RequestParam(name = "pageSize", defaultValue = "5") int pageSize) {
        FactoryProductExample factoryProductExample = new FactoryProductExample();
        factoryProductService.findAll(factoryProductExample);
        PageInfo pageInfo= factoryProductService.findByPage(pageNum,pageSize,factoryProductExample);
        request.setAttribute("page", pageInfo);
        return "cargo/factoryProduct/factoryProduct-list";
    }

    @RequestMapping(value = "/toAdd", name = "产品新增页面")
    public String toAdd() {
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        return "cargo/factoryProduct/factoryProduct-edit";
    }

    @RequestMapping(value = "/toUpdate", name = "产品修改页面")
    public String toUpdate(String id) {
        FactoryProduct factoryProduct = factoryProductService.findById(id);

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryProduct", factoryProduct);
        request.setAttribute("factoryList", factoryList);
        return "cargo/factoryProduct/factoryProduct-edit";
    }

    @RequestMapping(value = "/edit", name = "产品新增或修改")
    public String edit(FactoryProduct factoryProduct) {
        if (StringUtils.isEmpty(factoryProduct.getId())) {
            factoryProduct.setId(UUID.randomUUID().toString());
            factoryProduct.setCreateBy(getLoginUser().getCreateBy());
            factoryProduct.setCreateDept(getLoginUser().getCreateDept());
            factoryProduct.setCreateTime(new Date());
            factoryProduct.setInputTime(new Date());
            factoryProductService.save(factoryProduct);
        } else {
            factoryProductService.update(factoryProduct);
        }
        return "redirect:/cargo/factoryProduct/list.do?id=" + factoryProduct.getFactoryId();
    }

    @RequestMapping(value = "/delete", name = "产品删除")
    public String delete(String id) {
        factoryProductService.delete(id);
        return "redirect:/cargo/factoryProduct/list.do";
    }
}
